<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width,user-scalable=no,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<title>{echo admin_title()}</title>
<!--header-->
{template _:header}
</head>
<body>
<div class="layui-layout layui-layout-admin">
<!--top-->
{template _main_top}
<!--nav-->
{template _main_nav}
	<div class="position"><h1>商户列表</h1></div>
	<div class="layui-body admin-body">
		<div class="admin-content">
			<div class="searcher">
				<form name="sch_form" id="sch_form" method="get" action="{echo \befen\get_url()}">
					<div class="layui-input-inline"><input type="text" name="agent_name" id="agent_name" class="layui-input" value="{echo input('param.agent_name')}" placeholder="代理编号或名称"></div>
					<div class="layui-input-inline"><input type="text" name="merchant_name" id="merchant_name" class="layui-input" value="{echo input('param.merchant_name')}" placeholder="商户编号或名称"></div>
					<div class="layui-input-inline layui-left-margin"><button type="submit" id="search" class="layui-btn">查询</button></div>
					<div class="layui-input-inline layui-left-margin"><button type="submit" name="action" value="export" class="layui-btn">导出</button></div>
				</form>
				<div class="clear"></div>
			</div>
			<form name="theform" id="theform" method="post" action="{echo \befen\get_url()}">
			<table class="layui-table">
				<thead>
					<tr>
						<th>商户名称</th>
						<th>代理名称</th>
						<th>申请时间</th>
						<th>商户状态</th>
						<th>默认费率</th>
						<th style="display:none">微信绑定</th>
						<th>操作</th>
					</tr>
				</thead>
				<tbody>
					<!--{loop $list $key $val}-->
					<tr id="">
						<td><a class="goto_panel" href="javascript:;" merchant_id="{$val[merchant_id]}" merchant_url="{echo url('/merchant/login/console', ['merchant_id' => '__authid__'])}">{$val[merchant_name]}</a></td>
						<td>{$val[agent_name]}</td>
						<td>{php}echo gsdate('Y-m-d', $val['time_create']){/php}</td>
						<td class="op"><span op="status" val="{$val[status]}" lay-url="{echo url('merchant/status', ['merchant_id' => $val['merchant_id']])}" class="layui-badge layui-bg-status-{$val[status]}">{echo model('Merchant')->getStatus($val['status'])}</span></td>
						<td class="op"><span op="trade_rates" val="{$val[merchant_id]}" class="layui-badge layui-badge-rim">{echo number_format($val['trade_rates'], 2)}%</span></td>
						<td style="display:none" id="weixin_{$val[merchant_id]}"><!--{if $val['openid']}--><!--{if $val['nickname']}-->{$val[nickname]}<!--{else}-->是<!--{/if}--><!--{else}-->-<!--{/if}--></td>
						<td class="op">
							<a op="detail" lay-url="{echo url('merchant/detail', ['merchant_id' => $val['merchant_id']])}" class="layui-btn layui-btn-xs layui-btn-normal">商户详情</a>
							<a op="device" lay-url="{echo url('device/index', ['merchant_id' => $val['merchant_id']])}" class="layui-btn layui-btn-xs layui-btn-normal">终端设备</a>
							<a op="auth" merchant_id="{$val[merchant_id]}" merchant_name="{$val[merchant_name]}" class="layui-btn layui-btn-xs layui-btn-normal">商户授权</a>
							<a op="passwd" lay-url="{echo url('merchant/passwd', ['merchant_id' => $val['merchant_id']])}" class="layui-btn layui-btn-xs layui-btn-normal">重置密码</a>
							<!--{if !$val['openid']}-->
							<a style="display:none" op="bind_wechat" merchant_id="{$val[merchant_id]}" merchant_name="{$val[merchant_name]}" lay-url="{echo url('merchant/bind_wechat', ['merchant_id' => $val['merchant_id']])}" class="layui-btn layui-btn-xs layui-btn-normal">绑定微信</a>
							<!--{else}-->
							<a style="display:none" op="unbind_wechat" merchant_id="{$val[merchant_id]}" merchant_name="{$val[merchant_name]}" lay-url="{echo url('merchant/unbind_wechat', ['merchant_id' => $val['merchant_id']])}" class="layui-btn layui-btn-xs layui-btn-normal">解绑微信</a>
							<!--{/if}-->
							<a op="gates" lay-url="{echo url('merchant/gates', ['merchant_id' => $val['merchant_id']])}" class="layui-btn layui-btn-xs layui-btn-normal">通道配置</a>
							<!--<a op="secret" lay-url="{echo url('merchant/secret', ['merchant_id' => $val['merchant_id']])}" class="layui-btn layui-btn-xs layui-btn-normal">密钥配置</a>-->
						</td>
					</tr>
					<!--{/loop}-->
				</tbody>
			</table>
			</form>
			<div class="listpages">{$pagenav}</div>
		</div>
	</div>
</div>
<!--footer-->
{template _:footer}
<style>
.layui-badge {cursor:pointer;}
</style>
<script type="text/javascript">
$("a[op=detail]").click(function(){
	showWindow('商户详情', $(this).attr('lay-url'), '800px');
});
$("a[op=device]").click(function(){
	gotourl($(this).attr('lay-url'));
});
$("a[op=passwd]").click(function(){
	showWindow('重置密码', $(this).attr('lay-url'), '300px');
});
$("a[op=unbind_wechat]").click(function(){
	var merchant_id = $(this).attr('merchant_id');
	op("确认要解绑吗？", $(this).attr('lay-url'), function(){
		reload();
	});
});
$("a[op=gates]").click(function(){
	showWindow('通道配置', $(this).attr('lay-url'), '500px');
});
$("a[op=secret]").click(function(){
	showWindow('密钥配置', $(this).attr('lay-url'), '800px');
});
$("span[op=status]").click(function(){
	var status = $(this).attr("val");
	if(status == 0) {
		op_msg = "开启";
	} else {
		op_msg = "禁用";
	}
	op("确认 " + op_msg + " 商户状态吗？", $(this).attr('lay-url'));
});
$(".goto_panel").click(function(e){
	e.preventDefault();
	e.stopPropagation();
	var _window = window.open("about:blank", "_blank");
	var merchant_id = $(this).attr("merchant_id");
	var merchant_url = $(this).attr("merchant_url");
	$.ajax({
		type: "POST",
		dataType: "json",
		timeout: 10000,
		url: "{echo url('merchant/get_id_authcode')}",
		data: {
			ajax: "json",
			merchant_id: merchant_id,
		},
		success: function(data){
			_window.blur();
			_window.opener.focus();
			_window.location = merchant_url.replace("__authid__", data.contents.merchant_id);
		},
		error: function(xhr, status){
			console.log(status);
			console.log(xhr);
		},
		complete: function(){
			hideLoader();
		},
		beforeSend: function(){
			showLoader();
		}
	});
});
$("a[op=auth]").click(function(){
	var merchant_id = $(this).attr('merchant_id');
	var merchant_name = $(this).attr('merchant_name');
	$.ajax({
		type: "POST",
		dataType: "html",
		timeout: 30000,
		url: "{echo url('merchant/get_url_auth_token')}",
		data: {
			ajax: "html",
			merchant_id: merchant_id,
		},
		success: function(html){
			init_Layer(function(){
				layer.open({
					title: '商户授权 - 支付宝 - ' + merchant_name,
					shade: 0.5,
					isOutAnim: true,
					content: '<div style="text-align:center"><div id="auth_qrcode" style="width:250px;height:250px;margin:0 auto 1em;"></div><h3>请引导商户扫描此二维码进行授权</h3></div>',
					btn: false
				});
				var qrcode = new QRCode(document.getElementById("auth_qrcode"), {
					width: 250,
					height: 250
				});
				qrcode.makeCode(html);
			});
		},
		complete: function(){
			hideLoader();
		},
		beforeSend: function(){
			showLoader();
		}
	});
});
$("span[op=trade_rates]").click(function(){
	var merchant_id = $(this).attr("val");
	var value = $(this).text().replace(/[^\d.]/g, "");
	init_Layer(function(){
		layer.prompt({
			title: "请输入商户结算费率",
			value: value,
		}, function(value, index){
			$.ajax({
				type: "POST",
				dataType: "json",
				cache: false,
				timeout: 30000,
				url: "{echo url('merchant/trade_rates')}",
				data: {
					ajax: "json",
					merchant_id: merchant_id,
					trade_rates: value,
				},
				success: function(data){
					if(data.status == 1) {
						showInfo(data.message, function(){
							reload();
						});
					} else {
						showAlert(data.message);
					}
				},
				error: function(xhr, status){
					console.log(xhr);
				},
				complete: function(){
					hideLoader();
				},
				beforeSend: function(){
					showLoader();
				}
			});
		});
	});
});
$('#sch_form input[name=agent_name]').autoComplete({
	url: "{echo url('agent/index')}",
	field: "agent_no",
	value: "{echo input('param.agent_name')}",
	name_hidden: "agent_id",
	value_hidden: "{echo input('param.agent_id')}",
});
$('#sch_form input[name=merchant_name]').autoComplete({
	url: "{echo url('merchant/index')}",
	field: "merchant_no",
	value: "{echo input('param.merchant_name')}",
	name_hidden: "merchant_id",
	value_hidden: "{echo input('param.merchant_id')}",
});
</script>
</body>
</html>